// Copyright 2012 dmitry.a.novikov@gmail.com
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// CnsZ::Object declaration


#ifndef _INCLUDED_CNZ_OBJECT_H_
#define _INCLUDED_CNZ_OBJECT_H_

#include "ptr.h"

namespace CnsZ {

  class Class;
  class Filer;
  class String;
  class AString;
  class ValueAString;
  class DObject;

#define DECLARE_CNSZ_CLASS(ClassName) \
    virtual const Class* isA() const;\
    static const Class* classInfo();\
    static P<ClassName> New()


  class Object {
  public:
    DECLARE_CNSZ_CLASS(Object);

    virtual ~Object();

    virtual void addRef() const = 0;

    virtual void release() const = 0;

    virtual int numRefs() const = 0;

    virtual P<Object> Interface( const Class* interfaceName ) const;

    virtual bool isKindOf( const Class* clsName ) const;

    virtual int compare(const Object* other) const;

    virtual void copy(const Object* from);

    P<Object> clone() const;

    virtual void read(Filer& filer);

    virtual void write(Filer& filer) const;

    // Returns "<Object@0000ADDB>"
    virtual String toString() const;
  };

  class Class : public Object {
  public:
    DECLARE_CNSZ_CLASS(Class);

    virtual const Class* parentClass() const = 0;

    virtual const AString& name() const = 0;

    virtual P<Object> createInstance() const = 0;
  };

  class ObjectIterator : public Object {
  public:
    DECLARE_CNSZ_CLASS(ObjectIterator);

    virtual bool done() const = 0;

    virtual P<Object> identifer() const = 0;

    virtual P<Object> object() const = 0;

    virtual P<Object> object() = 0;

    virtual void next() = 0;

    virtual size_t count() const = 0;

    virtual void seekStart() = 0;
  };

  template<class TTo>
  P<TTo> castTo( const Object* obj, const Class* toClass = TTo::classInfo() ) {
    if(obj==0) {
      return P<TTo>();
    }
    if(toClass==0) {
      throw NullPointer();
    }
    return const_cast<TTo*>( static_cast<const TTo*>(obj->Interface(toClass).get()) );
  }

}

std::wostream& operator<<(std::wostream& ostr, const CnsZ::Object* object);

#endif //#ifndef _INCLUDED_CNZ_OBJECT_H_